package org.mcb.zebra.portal.services;

import java.util.Iterator;
import java.util.Map;

import javax.portlet.PortletMode;
import javax.portlet.PortletSecurityException;
import javax.portlet.WindowState;
import javax.servlet.http.HttpServletRequest;

import org.apache.pluto.PortletWindow;
import org.apache.pluto.spi.PortletURLProvider;
import org.mcb.zebra.portal.manager.PortalManager;
import org.mcb.zebra.portal.manager.ZebPortalManager;
import org.mcb.zebra.portal.url.UrlParser;
import org.mcb.zebra.portal.window.ZebPortletWindow;

@SuppressWarnings("unchecked")
public class ZebPortletURLProvider implements PortletURLProvider {
	HttpServletRequest request;
	ZebPortletWindow portletWindow;
	boolean action = false;
	Map parameters;

	public ZebPortletURLProvider(HttpServletRequest request,
			PortletWindow portletWindow) {
		this.request = request;
		PortalManager manager = ZebPortalManager.getInstance();
		UrlParser urlParser = manager.getUrlParser();
		this.portletWindow = (ZebPortletWindow) (urlParser != null ? urlParser
				.getPortletWindow(request, portletWindow.getId().getStringId())
				: null);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.pluto.spi.PortletURLProvider#clearParameters()
	 */
	@Override
	public void clearParameters() {
		if (parameters != null)
			parameters.clear();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.pluto.spi.PortletURLProvider#isSecureSupported()
	 */
	@Override
	public boolean isSecureSupported() {

		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.pluto.spi.PortletURLProvider#setAction(boolean)
	 */
	@Override
	public void setAction(boolean action) {
		this.action = action;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.pluto.spi.PortletURLProvider#setParameters(java.util.Map)
	 */
	@Override
	public void setParameters(Map parameters) {
		this.parameters = parameters;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.pluto.spi.PortletURLProvider#setPortletMode(javax.portlet.PortletMode)
	 */
	@Override
	public void setPortletMode(PortletMode mode) {
		portletWindow.setPortletMode(mode);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.pluto.spi.PortletURLProvider#setSecure()
	 */
	@Override
	public void setSecure() throws PortletSecurityException {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.pluto.spi.PortletURLProvider#setWindowState(javax.portlet.WindowState)
	 */
	@Override
	public void setWindowState(WindowState state) {
		portletWindow.setWindowState(state);
	}

	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		buffer.append(request.getContextPath());
		buffer.append(request.getServletPath());
		buffer.append("/");
		if (portletWindow != null) {
			buffer.append(portletWindow.getId().getStringId());
			buffer.append("~");
			buffer.append(portletWindow.getPortletMode().toString());
			buffer.append("!");
			buffer.append(portletWindow.getWindowState().toString());
			if (action)
				buffer.append("~");
			if (parameters != null) {
				buffer.append("?");
				Iterator en = parameters.keySet().iterator();
				boolean first = true;

				while (en.hasNext()) {
					if (first)
						first = false;
					else
						buffer.append("&");
					Object elem = en.next();
					buffer.append(elem);
					buffer.append("=");
					if (elem != null
							&& ((String[]) parameters.get(elem))[0] != null)
						buffer.append(((String[]) parameters.get(elem))[0]);

				}

			}

		}
		return buffer.toString();

	}

}
